<?php
declare(strict_types=1);
namespace Service\GoodsService;
use Service\BaseService;
use App\Model\BbcModel\GoodsTags;

/**
 * 商品标签服务中心
 * @author chenshubo
 */
class GoodsTagsService extends BaseService
{
    /**
     * 获取标签数据
     * @param int $cid
     */
    public function getGoodsTagsList( int $cid)
    {
        return GoodsTags::instance()
            ->select([
                'id',
                'show_name',
                'img',
                'position'
            ])
            ->where('cid',$cid)
            ->where('show',1)
            ->get()
            ->toArray();
    }

    /**
     * 处理标签
     * @param array $goodsTagList
     * @param array $goods
     */
    public function getGoodsTags( array $goodsTagList,array $goods)
    {
        //商品标签
        $label=[];
        if( !empty($goodsTagList) ) {
            $tags=[];

            if(!empty($goods['tag'])){
                $tags=explode(',',$goods['tag']);
            }


            if(!empty($goods['supply_tag'])){
                $tags=array_merge($tags,explode(',',$goods['supply_tag']));
            }

            if(!empty($tags)){
                foreach ($goodsTagList as $v){
                    if(in_array($v['id'],$tags)){
                        $label[]=$v;
                    }
                }
            }
        }

        return $label;
    }
}